package com.zyj.report.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zyj.common.annotation.Log;
import com.zyj.common.core.controller.BaseController;
import com.zyj.common.core.domain.AjaxResult;
import com.zyj.common.core.domain.entity.SysUser;
import com.zyj.common.core.page.TableDataInfo;
import com.zyj.common.enums.BusinessType;
import com.zyj.common.utils.DateUtils;
import com.zyj.common.utils.http.HttpUtils;
import com.zyj.common.utils.poi.ExcelUtil;
import com.zyj.receive.domain.AssetsReceive;
import com.zyj.receive.service.IAssetsReceiveService;
import com.zyj.report.domain.AssetsReportRepairs;
import com.zyj.report.service.IAssetsReportRepairsService;
import com.zyj.system.service.ISysRoleService;
import com.zyj.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 维修报统计Controller
 * 
 * @author zhuyujie
 * @date 2022-05-14
 */
@Controller
@RequestMapping("/assets/repairsreport")
public class AssetsReportRepairsController extends BaseController
{
    private String prefix = "assets/repairsreport";

    @Autowired
    private IAssetsReportRepairsService assetsReportRepairsService;

    @Autowired
    private IAssetsReceiveService assetsReceiveService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping()
    public String repairs()
    {
        return prefix + "/repairsreport";
    }

    //每日凌晨1点计算维修报表数据
    @GetMapping("/getReportPrpairs")
    @ResponseBody
    public Map getReportPrpairs()
    {
        //按维修状态分组查询每个状态的数量
        String nowtime = DateUtils.getDate();
        AssetsReportRepairs assetsReportRepairs = new AssetsReportRepairs();
        AssetsReportRepairs arr = assetsReportRepairsService.selectAssetsReportRepairs();
        //查询当天是否有数据
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("beginCreateTime", nowtime + " 00:00:00");
        params.put("endCreateTime", nowtime + " 23:59:59");
        assetsReportRepairs.setParams(params);
        List<AssetsReportRepairs> report = assetsReportRepairsService.selectAssetsReportRepairsList(assetsReportRepairs);
        assetsReportRepairs.setAssetsAllNum(arr.getAssetsAllNum());
        assetsReportRepairs.setAllNum(arr.getAllNum());
        assetsReportRepairs.setRepairsNum(arr.getRepairsNum());
        assetsReportRepairs.setScrepNum(arr.getScrepNum());
        assetsReportRepairs.setDoneNum(arr.getDoneNum());
        assetsReportRepairs.setCreateTime(new Date());
        if(report!=null && !report.isEmpty()) {
            AssetsReportRepairs r = report.get(0);
            assetsReportRepairs.setId(r.getId());
            assetsReportRepairsService.updateAssetsReportRepairs(assetsReportRepairs);
        }else{
            assetsReportRepairsService.insertAssetsReportRepairs(assetsReportRepairs);
        }

        return new HashMap<>();
    }



    /**
     * 查询维修报统计列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AssetsReportRepairs assetsReportRepairs)
    {
        startPage();
        List<AssetsReportRepairs> list = assetsReportRepairsService.selectAssetsReportRepairsList(assetsReportRepairs);
        return getDataTable(list);
    }

    /**
     * 导出维修报统计列表
     */
    @Log(title = "维修报统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AssetsReportRepairs assetsReportRepairs)
    {
        List<AssetsReportRepairs> list = assetsReportRepairsService.selectAssetsReportRepairsList(assetsReportRepairs);
        ExcelUtil<AssetsReportRepairs> util = new ExcelUtil<AssetsReportRepairs>(AssetsReportRepairs.class);
        return util.exportExcel(list, "维修报统计数据");
    }

    /**
     * 新增维修报统计
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存维修报统计
     */
    @Log(title = "维修报统计", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AssetsReportRepairs assetsReportRepairs)
    {
        return toAjax(assetsReportRepairsService.insertAssetsReportRepairs(assetsReportRepairs));
    }

    /**
     * 修改维修报统计
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AssetsReportRepairs assetsReportRepairs = assetsReportRepairsService.selectAssetsReportRepairsById(id);
        mmap.put("assetsReportRepairs", assetsReportRepairs);
        return prefix + "/edit";
    }

    /**
     * 修改保存维修报统计
     */
    @Log(title = "维修报统计", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AssetsReportRepairs assetsReportRepairs)
    {
        return toAjax(assetsReportRepairsService.updateAssetsReportRepairs(assetsReportRepairs));
    }

    /**
     * 删除维修报统计
     */
    @Log(title = "维修报统计", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(assetsReportRepairsService.deleteAssetsReportRepairsByIds(ids));
    }


    //资产借用逾期报警
    @GetMapping("/getBorrowExp")
    @ResponseBody
    public Map getBorrowExp()
    {
        AssetsReceive assetsReceive = new AssetsReceive();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("beginPredictTime","2000-01-01");
        params.put("endPredictTime",DateUtils.getDate());
        assetsReceive.setParams(params);
        assetsReceive.setReceiveType("2");
        List<AssetsReceive> list = assetsReceiveService.selectAssetsReceivePredictedList(assetsReceive);
        if(list!=null && !list.isEmpty()){
            String userids = "";
            //获取资产管理员信息
            List<SysUser> userlist =  roleService.selectUserListByRoleId(4L);
            if(userlist!=null && !userlist.isEmpty()){
                for(int i=0;i<userlist.size();i++){
                    SysUser user = userlist.get(i);
                    String externalUserid = user.getExternalUserid();
                    userids+=externalUserid+",";
                }
            }


            for(int i=0;i<list.size();i++){
                AssetsReceive ar = list.get(i);
                String assetsName = ar.getAssetsName();
                String assetsId = ar.getAssetsId();
                String username = ar.getUserName();
                SysUser sysuser = sysUserService.selectUserById(ar.getUserId());
                String mobile = sysuser.getPhonenumber();
                String retime = DateUtils.dateTime(ar.getPredictTime());
                String msg = "资产逾期归还提醒：" +
                        "资产编号："+assetsId+"("+assetsName+")， " +
                        "借用人："+username+"("+mobile+")，" +
                        "应归还时间："+retime ;
                String json = "{\"userids\":\"&userids&\",\"msg\":\""+msg+"\"}";
                userids+=sysuser.getExternalUserid()+",";
                //给资产管理员和当事人发消息
                if(!"".equals(userids)){
                    userids=userids.substring(0, userids.length()-1);
                    json = json.replace("&userids&", userids);
                    System.out.println("json:"+json);
                    HttpUtils.sendPost("http://localhost/feishu/sendMsg/", "json=" + json);
                }

            }
        }
        return new HashMap<>();
    }
}
